package pers.gxm.interpreter;

import java.io.*;
import java.util.Scanner;

public class CommandNode extends Node{
    public static void main(String[] args) {
//        Scanner scanner = new Scanner(System.in);
//        int i = 1;
//        while (scanner.hasNext()){
//            System.out.println("第" + i++ + "次执行");
//            String s = scanner.next();
//            System.out.println("刚刚输入的是" + s);
//        }


//        System.out.println("请输入字符串，并多混入空格回车尝试不同结果");
//        if(scanner.hasNext() ) {
//            System.out.println("第一次" + scanner.next());
//            System.out.println("第二次" + scanner.nextLine());
//            System.out.println("第三次" + scanner.nextLine());
//        }

        InputStream is = CommandNode.class.getClassLoader().getResourceAsStream("interpreter/program.txt");
        try(BufferedReader reader = new BufferedReader(new InputStreamReader(is));){
            String text = null;
            while ((text = reader.readLine()) != null){
                System.out.println("text  = \"" + text + "\"");
                ProgramNode programNode = new ProgramNode();
                programNode.parse(new Context(text));
                System.out.println("node = " + programNode);
            }
        }catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
    private Node node;
    @Override
    public void parse(Context context) throws ParseException {
        if("repeat".equals(context.currentToken())){
            node = new RepeatCommandNode();
            node.parse(context);
        }else{
            node = new PrimitiveCommandNode();
            node.parse(context);
        }
    }

    public String toString(){
        return node.toString();
    }
}
